package cn.weekimwee.firstlineofcodeforkotlin.unit3

import android.app.AlertDialog
import android.app.ProgressDialog
import android.os.Bundle
import android.view.View
import cn.weekimwee.firstlineofcodeforkotlin.BaseActivity
import cn.weekimwee.firstlineofcodeforkotlin.R
import kotlinx.android.synthetic.main.activity_progress_bar.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class ProgressBarActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_progress_bar)
        button.setOnClickListener { it.click() }
        button2.setOnClickListener { it.click() }
        button3.setOnClickListener { it.click() }
        button4.setOnClickListener { it.click() }
        button5.setOnClickListener { it.click() }
        button6.setOnClickListener { it.click() }
        button7.setOnClickListener { it.click() }
        button8.setOnClickListener { it.click() }
    }

    private fun View.click() {
        when (this.id) {
            R.id.button -> {
                if (progress.visibility == View.GONE) {
                    progress.visibility = View.VISIBLE
                } else {
                    progress.visibility = View.GONE
                }
            }
            R.id.button2 -> {
                progress.progress = progress.progress + 10
            }
            R.id.button3 -> {
                AlertDialog.Builder(this@ProgressBarActivity)
                        .apply {
                            setTitle("this is dialog for app")
                            setMessage("something important")
                            setCancelable(false)
                            setPositiveButton("ok") { d, _ ->
                                toast("clicked ok")
                                d.dismiss()
                            }
                            setNegativeButton("cancel", { d, _ ->
                                toast("clicked cancel")
                                d.cancel()
                            })
                            setOnDismissListener {
                                toast("dismissListener")
                            }
                            setOnCancelListener {
                                toast("cancelListener")
                            }
                        }
                        .show()
            }
            R.id.button4 -> {
                android.support.v7.app.AlertDialog.Builder(this@ProgressBarActivity)
                        .apply {
                            setTitle("this is dialog for v7")
                            setMessage("something important")
                            setCancelable(true)
                            setPositiveButton("ok", { dialog, _ ->
                                dialog.dismiss()
                            })
                            setNegativeButton("cancel", { dialog, _ ->
                                dialog.dismiss()
                            })
                            setOnDismissListener {
                                toast("v7 dismiss")
                            }
                            setOnCancelListener {
                                toast("v7 cancel")
                            }
                        }
                        .show()
            }
            R.id.button5 -> {
                //8.0里过时了
                ProgressDialog(this@ProgressBarActivity)
                        .apply {
                            setTitle("this is progress dialog")
                            setMessage("loading...")
                            setCancelable(true)
                            show()
                        }
            }
            R.id.button6 -> {
                startActivity<PercentLayoutActivity>()
            }
            R.id.button7 -> {
                startActivity<UIActivity>()
            }
            R.id.button8 -> {
                startActivity<RecyclerViewActivity>()
            }
        }
    }
}
